﻿using System;
using System.Collections.Generic;
using System.Configuration;
using System.Data;
using System.Data.SqlClient;
using System.Text;
using Dzzzr.DataLayer;

namespace Dzzzr
{
  public partial class Marker
  {
    public static List<Marker> GetMarkerList(int gameID)
    {
      List<Marker> markers = new List<Marker>();
      using (SqlConnection cnn = Helper.Connection)
      {
        SqlCommand cmd = new SqlCommand("GetLastGPSPointByGameID", cnn);
        cmd.CommandType = CommandType.StoredProcedure;
        cmd.Parameters.AddWithValue("@GameID", gameID);
        SqlDataReader rd = cmd.ExecuteReader(CommandBehavior.CloseConnection);
        while (rd.Read())
        {
          Marker marker = new Marker();
          marker.ID = rd.GetInt32(0);
          marker.Name = rd.GetString(1);
          marker.Latitude = rd.GetDouble(2);
          marker.Longitude = rd.GetDouble(3);
          marker.Time = rd.GetDateTime(4);
          marker.Speed = rd.IsDBNull(5)? new Nullable<double>() : new Nullable<double>(rd.GetDouble(5));
          marker.Altitude = rd.IsDBNull(6) ? new Nullable<double>() : new Nullable<double>(rd.GetDouble(6));
          marker.Delay = rd.IsDBNull(7) ? new Nullable<double>() : new Nullable<double>(rd.GetInt32(7));
          markers.Add(marker);
        }
      }
      return markers;
    }


    public static void AddPoint(int entityID, int gameID, int userID, float latitude, float longitude, float altitude, int speed)
    {
      using (SqlConnection cnn = Helper.Connection)
      {
        SqlCommand cmd = new SqlCommand("InsertGPSPoint", cnn);
        cmd.CommandType = CommandType.StoredProcedure;
        cmd.Parameters.AddWithValue("@EntityID", entityID);
        cmd.Parameters.AddWithValue("@GameID", gameID);
        cmd.Parameters.AddWithValue("@UserID", userID);
        cmd.Parameters.AddWithValue("@Latitude", latitude);
        cmd.Parameters.AddWithValue("@Longitude", longitude);
        cmd.Parameters.AddWithValue("@Altitude", altitude);
        cmd.Parameters.AddWithValue("@Speed", speed);
        cmd.ExecuteNonQuery();
      }
    }


  }
}
